package com.project.thisDatabase.redis;

import com.project.thisCommon.common.ConvertEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

//@Component
public class RedisRunner {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    private static RedisTemplate<Object,Object> redisTemplateDao;

    // 初始化调用
    @PostConstruct
    public void init() {
        redisTemplateDao = redisTemplate;
    }

    public static <T> T getMap(String key, String name, Class cls){
        T result = null;
        if(redisTemplateDao.opsForHash().get(key,name) != null){
            String json = redisTemplateDao.opsForHash().get(key,name).toString();
            result = ConvertEntity.jsonToJava(json, cls);
        }
        return result;
    }

    public static String getStr(String key, String name){
        String result = null;
        if(redisTemplateDao.opsForHash().get(key,name) != null){
            result = redisTemplateDao.opsForHash().get(key,name).toString();
        }
        return result;
    }

    /**
     * 根据key查找记录
     * @param key
     * @return
     */
    public static String getByKey(String key){
        String result = null;
        if(redisTemplateDao.opsForValue().get(key) != null){
            result = redisTemplateDao.opsForValue().get(key).toString();
        }
        return result;
    }
}
